package cn.hr.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import cn.hr.pojo.SalaryStandard;
import cn.hr.pojo.SysUser;
import cn.hr.service.ArchivesService;
import cn.hr.service.DictionaryService;
import cn.hr.service.SalProStanService;
import cn.hr.service.SalaryProService;
import cn.hr.service.SysUserService;
import cn.hr.utils.Constants;
import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.hr.service.SalaryStandardService;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

/**
 * 薪酬标准控制器
 *
 * @author MO
 */
@Controller
@RequestMapping("/salaryStandard")
@PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_PAYROLL_MANAGER') or hasRole('ROLE_PAYROLL_ATTACHE')")
public class SalaryStandardController {
    private Logger logger = LoggerFactory.getLogger(SalaryStandardController.class);
    @Resource
    private SalaryStandardService salaryStandardService;
    @Resource
    private SalaryProService salaryProService;
    @Resource
    private SalProStanService salProStanService;
    @Resource
    private DictionaryService dictionaryService;
    @Resource
    private ArchivesService archivesService;
    @Resource
    private SysUserService sysUserService;

    @RequestMapping("/openAdd")
    public String openAdd(Model model) {
        logger.debug("openAdd--salaryStandard===================>");
        model.addAttribute("salaryStandard", new SalaryStandard());
        try {
            Map map = new HashMap();
            map.put("isStart", 1);
            model.addAttribute("salaryProList", salaryProService.findListByCondition(map, 0).getList());
        } catch (Exception e) {
            e.printStackTrace();
        }
        model.addAttribute("alter", "add");
        return "salaryStandard/AU";
    }

    @RequestMapping("/openUp/{id}/{str}")
    public String openUp(@PathVariable("id") String id, @PathVariable("str") String str, Model model) {
        logger.debug("openUp--salaryStandard======================>id");
        try {
            Map map = new HashMap();
            map.put("salS", id);
            SalaryStandard salaryStandard = salaryStandardService.get(Integer.valueOf(id));
            model.addAttribute("salaryStandard", salaryStandard);
            model.addAttribute("createdByName", sysUserService.get(salaryStandard.getCreatedById()).getUserName());
            model.addAttribute("salaryProList", salProStanService.findListByCondition(map, 0).getList());
        } catch (Exception e) {
            e.printStackTrace();
        }
        model.addAttribute("alter", "up");
        model.addAttribute("str", str);
        return "salaryStandard/AU";
    }

    @RequestMapping("/saveAddOrUp/{alter}")
    public String saveAddOrUp(@PathVariable("alter") String alter, SalaryStandard salaryStandard, HttpSession session, HttpServletRequest request) {
        logger.debug("saveAddOrUp--salaryStandard======================>alter,salaryStandard");
        try {
            if (alter.equals("add")) {
                salaryStandard.setCreatedById(findIdBySession(request));
                if (salaryStandardService.insert(salaryStandard)) {
                    session.setAttribute("message", "新增成功!");
                } else {
                    session.setAttribute("message", "新增失败!");
                }
            } else {
                salaryStandard.setModifyById(findIdBySession(request));
                if (salaryStandardService.update(salaryStandard)) {
                    session.setAttribute("message", "修改成功!");
                } else {
                    session.setAttribute("message", "修改失败!");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:/salaryStandard/openList";
    }

    @RequestMapping("/openList")
    public String openList(Model model) {
        try {
            model.addAttribute("status", dictionaryService.selectAllByTypeName("AUDITSTATUS_TYPE"));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return "salaryStandard/list";
    }

    @RequestMapping("/showList")
    public String showList(@RequestParam(required = false) Map map, @RequestParam("pageNo") String pageNo, Model model) {
        try {
            model.addAttribute("pageInfo", salaryStandardService.findListByCondition(map, Integer.parseInt(pageNo)));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "salaryStandard/showList";
    }

    @RequestMapping("/del/{id}")
    @ResponseBody
    public String del(@PathVariable("id") String id) {
        Map map = new HashMap();
        map.put("salS", id);
        try {
            if (archivesService.findCountByCondition(map) > 0) {
                map.put("result", "薪酬标准被引用,不能删除!");
            } else {
                if (salaryStandardService.delete(Integer.valueOf(id))) {
                    map.put("result", "true");
                } else {
                    map.put("result", "false");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return JSON.toJSONString(map);
    }

    public Integer findIdBySession(HttpServletRequest request) {
        return ((SysUser) request.getSession().getAttribute(Constants.USER_SESSION)).getId();
    }
}
